import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import GoodsList from '../components/goods/GoodsList.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'

Vue.use(VueRouter)

// const routes = [
//   {
//     path: '/',
//     name: 'Home',
//     component: Home
//   },
//   {
//     path: '/about',
//     name: 'About',
//     // route level code-splitting
//     // this generates a separate chunk (about.[hash].js) for this route
//     // which is lazy-loaded when the route is visited.
//     component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
//   }
// ]

// const router = new VueRouter({
//   routes
// })
// // 暴露出 router
// export default router

// export default new VueRouter({
// 	routes: [
// 		// 重定向路由
// 		{path: '/', redirect: '/login'},
// 		// 添加login路由规则
// 		{path: '/login', component: Login},
// 		// 添加home路由规则
// 		{path: '/home', component: Home}

// 	]
// })

const router = new VueRouter({
  routes: [
    // 重定向路由
    {
      path: '/',
      redirect: '/login'
    },
    // 添加 login 路由规则
    {
      path: '/login',
      component: Login
    },
    // 添加 home 路由规则, 添加 welcome 子路由规则, 并为它重定向路由在右部内容主体中
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        // 添加 users 路由规则
        {
          path: '/users',
          component: Users
        },
        // 添加 rights 路由规则
        {
          path: '/rights',
          component: Rights
        },
        // 添加 roles 路由规则
        {
          path: '/roles',
          component: Roles
        },
        // 添加 cate 路由规则
        {
          path: '/categories',
          component: Cate
        },
        // 添加 params 路由规则
        {
          path: '/params',
          component: Params
        },
        // 添加 goods 路由规则
        {
          path: '/goods',
          component: GoodsList
        },
        // 添加 add 路由规则
        {
          path: '/goods/add',
          component: Add
        },
        // 添加 order 路由规则
        {
          path: '/order',
          component: Order
        }
      ]
    }

  ]
})
// 挂载路由导航守卫
// 为路由对象, 添加 beforeEach 导航守卫
// router.beforeEach((to, form, next) => {
// 	// to 将要访问的路径
// 	// form 代表从哪个路径跳转而来
// 	// next 是一个函数,表示放行
// 	// next()放行, next('/login') 强制跳转
// 如果用户访问的登录页, 直接放行
// 	if (to.path === '/login') return next()
// 	// 从 sessionStorage 中获取到保存的 token 值
// 	const tokenStr = window.sessionStorage.getItem('token')
// 没有 token, 强制跳转到登录页面
// 	if (!tokenStr) return next('/login')
// 	next()
// })

export default router
